#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <time.h>
#include "netconnect.h"
#include "temper.h"

#define PORT 8080
#define BUFFER_SIZE 1024
#define LOCAL_IP "127.0.0.1"
#define MAX_RETRIES 3
#define RETRY_DELAY 5
#define NET_DISCONNECTED 0
#define NET_CONNECTED    1
#define NET_ERROR        -1




int network_init(NetworkContext *ctx) {
    ctx->sock_fd = -1;
    ctx->status = NET_DISCONNECTED;
    ctx->last_error = 0;
    ctx->last_connected = 0;
    return 0;
}

int network_check(NetworkContext *ctx) {
    if (ctx->sock_fd != -1) {
        // 简单检查连接是否仍然有效
        char buf[1];
        if (recv(ctx->sock_fd, buf, sizeof(buf), MSG_PEEK | MSG_DONTWAIT) < 0) {
            if (errno != EAGAIN && errno != EWOULDBLOCK) {
                ctx->status = NET_DISCONNECTED;
                close(ctx->sock_fd);
                ctx->sock_fd = -1;
            }
        }
    }
    return ctx->status;
}

int network_connect(NetworkContext *ctx, int max_retries, int retry_delay) {
    int attempts = 0;
    
    while (attempts < max_retries) {
        if (ctx->sock_fd != -1) {
            close(ctx->sock_fd);
        }
        
        ctx->sock_fd = socket(AF_INET, SOCK_STREAM, 0);
        if (ctx->sock_fd < 0) {
            ctx->last_error = errno;
            attempts++;
            sleep(retry_delay);
            continue;
        }

        struct sockaddr_in serv_addr = {
            .sin_family = AF_INET,
            .sin_port = htons(PORT)
        };
        
        if (inet_pton(AF_INET, LOCAL_IP, &serv_addr.sin_addr) <= 0) {
            ctx->last_error = errno;
            close(ctx->sock_fd);
            ctx->sock_fd = -1;
            attempts++;
            sleep(retry_delay);
            continue;
        }

        if (connect(ctx->sock_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
            ctx->last_error = errno;
            close(ctx->sock_fd);
            ctx->sock_fd = -1;
            attempts++;
            sleep(retry_delay);
            continue;
        }

        ctx->status = NET_CONNECTED;
        ctx->last_connected = time(NULL);
        return NET_CONNECTED;
    }
    
    ctx->status = NET_DISCONNECTED;
    return NET_DISCONNECTED;
}

int network_send(NetworkContext *ctx, SensorData *data) {
    if (ctx->status != NET_CONNECTED) {
        return -1;
    }
    
    char json_data[1024];
    snprintf(json_data, sizeof(json_data),
             "{\"timestamp\":\"%s\",\"temperature\":%.2f,\"humidity\":%.1f,\"sensor_id\":\"%s\"}",
             data->timestamp, data->temperature, data->humidity, data->sensor_id);

    if (send(ctx->sock_fd, json_data, strlen(json_data), 0) < 0) {
        ctx->status = NET_DISCONNECTED;
        close(ctx->sock_fd);
        ctx->sock_fd = -1;
        return -1;
    }

    return 0;
}

void network_close(NetworkContext *ctx) {
    if (ctx->sock_fd != -1) {
        close(ctx->sock_fd);
        ctx->sock_fd = -1;
    }
    ctx->status = NET_DISCONNECTED;
}


